package ywpt.web.action.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.ClientInfo;
import ywpt.service.client.ClientService;
import ywpt.service.contract.ContractService;
import ywpt.web.common.CommonDictionary;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ClientAction extends BaseAction implements
		ModelDriven<ClientInfo>, Preparable {

	/**
	 *  客户管理Action
	 */
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];//λȨ�����飬
	private static final long serialVersionUID = 16546132186465165L;
	private static int DEFAULT_PAGE_SIZE = 100;
	private Long id;

	private Long typeId;
	private String typeName;
	
	private ClientInfo clientInfo;
	private ClientService clientService;
	private List<ClientInfo> clientInfoList = new ArrayList<ClientInfo>();
		
	private ContractService contractService;
	
	
	//��ѯ���
	private String qClientCode;
	private String qClientName;
	private Long   qType;
	private String qAddress;
    

 

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	public ClientInfo getClientInfo() {
		return clientInfo;
	}

	public void setClientInfo(ClientInfo clientInfo) {
		this.clientInfo = clientInfo;
	}

	public ClientService getClientService() {
		return clientService;
	}

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public List<ClientInfo> getClientInfoList() {
		return clientInfoList;
	}

	public void setClientInfoList(List<ClientInfo> clientInfoList) {
		this.clientInfoList = clientInfoList;
	}
	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getQClientCode() {
		return qClientCode;
	}

	public void setQClientCode(String clientCode) {
		qClientCode = clientCode;
	}

	public String getQClientName() {
		return qClientName;
	}

	public void setQClientName(String clientName) {
		qClientName = clientName;
	}

	public Long getQType() {
		return qType;
	}

	public void setQType(Long type) {
		qType = type;
	}

	public String getQAddress() {
		return qAddress;
	}

	public void setQAddress(String address) {
		qAddress = address;
	}
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	// **************************��ݽ�������*******************************
	@Override
	public ClientInfo getModel() {
		return this.clientInfo;
	}

	@Override
	@SuppressWarnings("unchecked")
	public String execute() {
		return null;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			this.clientInfo = clientService.getClientInfoById(id);
		} else {
			this.clientInfo = new ClientInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_CLIENT_INFO);
	}
	protected void getDataList(DetachedCriteria query) {
		clientInfoList.clear();
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = clientService.getClientInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("clientCode"));
		query.setProjection(null);
		clientInfoList.addAll(clientService.getClientInfos(query, pageSize,rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);		
	}
	protected void showInfo() {
		if(clientInfo.getType() != null){
			this.setTypeId(clientInfo.getType().getId());
			this.setTypeName(clientInfo.getType().getCodeName());
		}
	}
	//------------------------------------------------------------------------------------------------
	public String list() {
		
		DetachedCriteria query = DetachedCriteria.forClass(ClientInfo.class);
		getDataList(query);
		return SUCCESS;
	}

	public String input() 
	{
		String code=clientService.createClientCode();
		clientInfo.setClientCode(code);
		return INPUT;
	}
	
	public String edit() 
	{
		this.clientInfo=this.clientService.getClientInfoById(id);
		showInfo();
		return INPUT;
	}
	
	/**
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				clientInfo.setClientCode(this.clientService.createClientCode());
				clientService.saveClientInfo(clientInfo,typeId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} 
			else 
			{
				clientService.updateClientInfo(clientInfo,typeId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * ɾ��
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			clientService.removeClientInfo(id);
			return SUCCESS;
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}
	
	
	public String search() throws Exception 
	{
		clientInfoList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(ClientInfo.class,"client");
		
		if(!StringTools.isNullOrSpace(this.qClientCode)){
			query.add(Restrictions.like("clientCode","%"+this.qClientCode+"%"));
		}
		
		if(this.qClientName != null && this.qClientName != ""){
			query.add(Restrictions.like("clientName", "%"+this.qClientName+"%"));
		}
		if(this.qType != null){
			query.createAlias("type","type").add(
					Restrictions.eq("type.id",this.qType));
		}	
		if(this.qAddress != null && this.qAddress != ""){
			query.add(Restrictions.like("address", "%"+this.qAddress+"%"));
		}	
		getDataList(query);
		return SUCCESS;
	}
	
	public String select() 
	{
		clientInfoList.addAll(clientService.getClientInfos());
		return SUCCESS;
	}
	

}
